package com.sx.pipeline.ui.map

import android.app.Application
import androidx.databinding.ObservableField
import com.amap.api.maps.model.LatLng
import com.blankj.utilcode.util.ThreadUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.DistrictBorderDTO
import com.sx.pipeline.utils.kml.FileParseBean
import okhttp3.internal.wait

/**
 * @Author: YY
 * @Date: 2022/8/29
 * @Desc: TODO
 */
class DistrictBorderVM(app: Application): CYBaseViewModel(app) {

    val districtBorderEvent: SingleLiveEvent<Void> = SingleLiveEvent()

//    fun addDistrictBorder(fileName: String, points: ArrayList<LatLng>) {
//        val nodesBeanList: MutableList<FileParseBean.NodesBean> = mutableListOf()
//        val fileParseBean = FileParseBean()
//        fileParseBean.name = fileName
//        fileParseBean.type = 0
//        Logs.d("addDistrictBorder ${points.size}")
//        points.forEach { mLatLng ->
//            val nodesBean: FileParseBean.NodesBean =
//                FileParseBean.NodesBean(mLatLng.longitude, mLatLng.latitude)
//            nodesBeanList.add(nodesBean)
//        }
//        fileParseBean.nodesBeans = nodesBeanList
//
//        addSubscribe(mModel.addTerritory(fileParseBean)
//            .compose(RxThreadHelper.rxSchedulerHelper())
//            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
//                (this, true) {
//                override fun onResult(result: Any) {
//                    ToastUtils.showShort("添加成功")
//                    districtBorderEvent.call()
//                }
//
//                override fun onFailed(code: Int, msg: String?) {
//                    super.onFailed(code, msg)
//                    ToastUtils.showShort(msg)
//                }
//            })
//        )
//    }

    var provinceIDField: ObservableField<Long> = ObservableField(-1)
    var cityIDField: ObservableField<Long> = ObservableField(-1)
    var areaIDField: ObservableField<Long> = ObservableField(-1)
    var provinceNameField: ObservableField<String> = ObservableField("")
    var cityNameField: ObservableField<String> = ObservableField("")
    var areaNameField: ObservableField<String> = ObservableField("")
    var addressDetailField: ObservableField<String> = ObservableField("")
    var nameField: ObservableField<String> = ObservableField("")

    fun addDistrictBorder() {
        val dto = DistrictBorderDTO(
            provinceIDField.get()!!.toLong(),
            cityIDField.get()!!.toLong(),
            areaIDField.get()!!.toLong(),
            provinceNameField.get()!!.toString(),
            cityNameField.get()!!.toString(),
            areaNameField.get()!!.toString(),
            addressDetailField.get()!!.toString(),
            nameField.get()!!.toString(),
        )

        addSubscribe(
            mModel.addDistrictBorder(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("添加成功")
                        districtBorderEvent.call()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

}